
*********************
* Authors: Henrik Andersson & Sirus Dehdari
* this do-file uses matched individual/workplace data
* and prepares the individual share of foreign born coworkers as well 
* as other measures at the worklace level.
* It generates three files (one for 2006, 2010 and 2014) with all individuals 
* who are connected to a workplace, and their respecitve share of foreign born 
* at the workplace. This do-file hence takes out the basic info for the treatment 
* variable, which will be collapsed at the precinct level at next stage. 
*********************

clear all
set more off

cd "D:\SCB_ConPol\Stata"

///Before start: socioeconomic data includes variables with nonlinear time addition 
///to variable name - change and save temp file before loop

use "LISA\LISA_2006"
rename AstSNI2002 AstSNI
save  "E:\ProjData\IntegrationSD\temp\LISA2006.dta", replace
use "LISA\LISA_2010"
rename AstSNI2007 AstSNI
save  "E:\ProjData\IntegrationSD\temp\LISA2010.dta", replace

*2014: must add SSYK data from key prepared before
use "LISA\LISA_2014"
rename AstSNI2007 AstSNI

merge m:1 ssyk4_2012 using "E:\ProjData\IntegrationSD\raw\nyckelssyk12_96"
rename SSYK96 SSYK4_96
drop _merge
save  "E:\ProjData\IntegrationSD\temp\LISA2014.dta", replace



*WORKPLACE DATA FOR 2006, 2010 AND 2014
forvalues y = 2006(4)2014 {

use "E:\ProjData\IntegrationSD\temp\LISA`y'.dta"



///first: only interested in those actually working at a workplace
///therefore drop unemployed and self-employed
///and those not connected to a workplace

keep if Yrkstalln==2 
drop if ArbstId==.


///throw out duplicates
duplicates tag LopNr, gen(tag)
tab tag
drop if tag==1
*2006: none
*2010: 2 ind/4 obs deleted*
*2014: 2 ind/4 obs deleted*
drop tag


///ADD Birth country data, need to define foreign born/non-European
merge 1:1 LopNr using "RTB\FodelselandIFAUGrupp"
	keep if _merge==3 
	drop _merge
	
	
///merge with migration data - used for describing years in country
merge 1:m LopNr using "RTB\migrationer"
*drop those in migration data but not in master
drop if _merge==2
*duplicates will remain 
*Generate year, month of arrival - throw out all observations before year in question 

tostring Datum, replace
gen im_year = substr(Datum, 1, 4) 
destring im_year, replace

gen im_month = substr(Datum, 5, 2) 
destring im_month, replace

duplicates drop LopNr if IFAUkod==0, force

*now sort on im year, duplicates drop takes out all but first obs, 
*which means that it takes out all but first im year

////DUBBELKOLLA SÅ ATT DU INTE FÅR MED KONSTIGA SOM UTVANDRAR FÖRST
sort LopNr im_year
duplicates drop LopNr, force

	*duplicates report LopNr /* OK*/
	

///ADD AGE AND SEX
///merge with migration data
drop _merge
merge 1:m LopNr using "RTB\Fodelseuppg", keepusing(LopNr FodArMan Kon)
keep if _merge==3
drop _merge

*Birth year
tostring FodArMan, replace
gen birth_year = substr(FodArMan, 1, 4) 
destring birth_year, replace

gen age = `y'-birth_year


////////////////////////////////////////////
////NOW THE FOLLOWING LINE OF CODES CREATES 
///THE MAIN TREATMENT; SHARE OF IM 
//// COWORKERS AMONG SWEDED
////////////////////////////////////////////	


///Start by defining being a native or comming from certain region
gen swedish = 1 if IFAUkod==0
gen nordic = 1 if IFAUkod==26 | IFAUkod==27 | IFAUkod==28
gen western = 1 if IFAUkod == 32 | IFAUkod == 33 | IFAUkod == 34 | IFAUkod == 38 | IFAUkod == 39
gen europe_other = 1 if IFAUkod == 29 | IFAUkod == 30 | IFAUkod == 31 | IFAUkod == 35 | IFAUkod == 36 | IFAUkod == 37
gen other =  1 if IFAUkod >39 & IFAUkod < 54
gen latinamerica = 1 if IFAUkod==40 | IFAUkod==41 | IFAUkod==42
gen mena = 1 if IFAUkod==44 | IFAUkod==46 | IFAUkod==47 | IFAUkod==48
gen asia = 1 if IFAUkod==49 | IFAUkod==50 | IFAUkod==51
gen africa = 1 if IFAUkod==43 | IFAUkod==45
gen rest_other = 1 if IFAUkod==52 | IFAUkod==53

///Now workplace codes, by workplace (ArbstId) , to gen # im coworkers vs # coworkers per workplace
*all workers at a workplace
bysort ArbstId: gen coworkers = _N

*all non-European workers at a workplace
bysort ArbstId: egen im_coworker = total(other)

*separated by region
bysort ArbstId: egen latin_coworker = total(latinamerica)
bysort ArbstId: egen mena_coworker = total(mena)
bysort ArbstId: egen asia_coworker = total(asia)
bysort ArbstId: egen africa_coworker = total(africa)
bysort ArbstId: egen rest_coworker = total(rest_other)
bysort ArbstId: egen nordic_coworker = total(nordic)
bysort ArbstId: egen western_coworker = total(western)


///Now that we have created the number of workers at workplace
* also want to throw out those who are alone
///note that since I have thrown out self-employed, this may sometimes be 1 employed plus the boss

drop if coworkers==1

///now condition the created workplace variables on the worker being swedish
*this creates the main treatment variable, share of (im)coworkers if you are born in Sweden
foreach x in im latin mena asia africa rest nordic western {
gen share_`x'_cworker = `x'_coworker/coworkers
gen share_`x'_cworker_swe = share_`x'_cworker if IFAUkod==0
gen `x'_coworker_swe = `x'_coworker if IFAUkod==0
}

*Total coworkers if you are swedish
gen coworkers_swe = coworkers if IFAUkod==0

*to create "CO"-workers, rather than workers, take coworkers - oneself; 
* "coworkers" otherwise captures all workers
gen cw_not_i = coworkers-1
gen sh_im_cw_noi = im_coworker/cw_not_i
gen sh_im_cw_noi_swe = sh_im_cw_noi if IFAUkod==0


/////////////////////////////////////////
//NOW VARIABLES USED AS CONTROLS; 
// MEAN WAGE, AGE AND SEX AT WORKPLACE 
///////////////////////////////////////

///share young co-workers
gen young_cw = 1 if age<30 

///per workplace
bysort ArbstId: egen young_cw_wp = total(young_cw)
gen share_young_cw_wp= young_cw_wp/coworkers

///share male coworkers? 
gen male_cw = 1 if Kon==1

bysort ArbstId: egen male_cw_wp = total(male_cw)
gen share_male_cw_wp= male_cw_wp/coworkers

///wage at workplace
bysort ArbstId: egen wage_cw_wp = mean(LoneInk)



///////////////////////////////
//NOW SEPARATE TREATMENT BASED
//ON SKILL LEVEL
/////////////////////////

///high and low skilled use first digit in SSYK
capture gen SSKY_str = substr(SSYK3, 1,1)
capture gen SSKY_str = substr(ssyk3_2012, 1,1)
encode SSKY_str, gen(SSKY1)
gen high_skilled=1 if SSKY1==1 | SSKY1==2 | SSKY1==3
gen low_skilled =1 if SSKY1>3 & SSKY1!=. 

///high and low skilled immigrants
gen highsk_other = 1 if high_skilled==1 & other==1
gen lowsk_other = 1 if low_skilled==1 & other==1

///per workplace
bysort ArbstId: egen highsk_coworker = total(high_skilled)
bysort ArbstId: egen lowsk_coworker = total(low_skilled)
bysort ArbstId: egen highskother_coworker = total(highsk_other)
bysort ArbstId: egen lowskother_coworker = total(lowsk_other)

///now condition the created workplace variables on being swedish
foreach x in highsk lowsk highskother lowskother  {
gen share_`x'_cworker = `x'_coworker/coworkers
gen share_`x'_cworker_swe = share_`x'_cworker if IFAUkod==0
gen `x'_coworker_swe = `x'_coworker if IFAUkod==0
}

///generate a matched skill level - that is, calculate the 
*number of imm coworkers within the same skill cell
gen highskillmatch = highskother_coworker if IFAUkod==0 & high_skilled==1
gen lowskillmatch = lowskother_coworker if IFAUkod==0 & low_skilled==1

gen share_matchedhigh_cworker = highskillmatch/highsk_coworker
gen share_matchedlow_cworker = lowskillmatch/lowsk_coworker

gen matchedcoworkers = share_matchedhigh_cworker
replace matchedcoworkers = share_matchedlow_cworker if matchedcoworkers==.

gen crossskill_highnative = lowskother_coworker if IFAUkod==0 & high_skilled==1
gen crossskill_lownative = highskother_coworker if IFAUkod==0 & low_skilled==1

gen sharecrossskill_highnative = crossskill_highnative/lowsk_coworker
gen sharecrossskill_lownative = crossskill_lownative/highsk_coworker 
  
gen crossskill = sharecrossskill_highnative
replace crossskill = sharecrossskill_lownative if crossskill==.

*Alternative 
gen hsnew = highsk_coworker-1 if high_skilled==1
gen lsnew = lowsk_coworker-1 if low_skilled==1

gen share_match_high_cworker = highskillmatch/hsnew
gen share_match_low_cworker = lowskillmatch/lsnew

gen matchedcw = share_match_high_cworker if high_skilled==1
replace matchedcw = share_match_low_cworker if low_skilled==1

*Do one where we divide the number of matched with total coworkers
gen share_matchedhigh_allcworker = highskillmatch/coworkers
gen share_matchedlow_allcworker = lowskillmatch/coworkers

gen matchedcoworkers_all = share_matchedhigh_allcworker
replace matchedcoworkers_all = share_matchedlow_allcworker if matchedcoworkers_all==.

 
 ///per workplace - create 1 digit SSYK variable
tab SSKY1, gen(SSYK1)

foreach b of numlist 1(1)11 {
gen SSYK1`b'other = SSYK1`b' if other==1
}

foreach b of numlist 1(1)11 {
bysort ArbstId: egen SSYK1`b'_coworker = total(SSYK1`b')
bysort ArbstId: egen SSYK1`b'_othercw = total(SSYK1`b'other)
}

///drop oneself, so we divide with CO-workers
foreach b of numlist 1(1)11 {
replace SSYK1`b'_coworker = SSYK1`b'_coworker-1 if SSYK1`b' ==1 
}

///now take share and condition the created workplace variables on being swedish
foreach b of numlist 1(1)11 {
gen share_ssyk1`b'_cworker = SSYK1`b'_othercw/SSYK1`b'_coworker
gen share_ssyk1`b'_cworker_swe = share_ssyk1`b'_cworker if IFAUkod==0
}

///one variable
gen share_ssyk1=.

foreach b of numlist 1(1)11 {
replace share_ssyk1 = share_ssyk1`b'_cworker_swe if SSYK1`b' ==1 
}

///not in the same occupation code
gen non_ssyk1 =.
foreach b of numlist 1(1)11 {
replace non_ssyk1 = im_coworker-SSYK1`b'_othercw if SSYK1`b' ==1 & IFAUkod==0
}

///standardize with all coworkers not in the same occupation
foreach b of numlist 1(1)11 {
replace SSYK1`b'_coworker = SSYK1`b'_coworker+1 if SSYK1`b' ==1 
}

gen non_ssyk1_all =.
foreach b of numlist 1(1)11 {
replace non_ssyk1_all = coworkers-SSYK1`b'_coworker if SSYK1`b' ==1 & IFAUkod==0
}
gen share_non_ssyk1 = non_ssyk1/non_ssyk1_all


///missing info on some   
replace share_ssyk1=. if SSYK11==1 
replace share_non_ssyk1=. if SSYK11==1 





/////////////////////////////////////////
//NOW CREATE DIFFERING TREATMENTS 
///DEPENDING ON SIZE OF WORKPLACE
///////////////////////////////////////

cumul coworkers_swe, gen(cumulworkers)
gen share_im_cworker_swe_smwp = sh_im_cw_noi_swe if cumulworkers<0.5 & cumulworkers!=.
gen share_im_cworker_swe_bigwp = sh_im_cw_noi_swe if cumulworkers>0.5 & cumulworkers!=.


gen share_matched_big=  matchedcw if cumulworkers>0.5 & cumulworkers!=.
gen share_matched_small=  matchedcw if cumulworkers<0.5 & cumulworkers!=.
gen share_cross_big= crossskill if cumulworkers>0.5 & cumulworkers!=.
gen share_cross_small= crossskill if cumulworkers<0.5 & cumulworkers!=.


*DIVIDE THE WORKPLACE INTO BIG AND SMALL WORKPLACES BASED ON SHARE OF 
*SAME SKILL WORKERS
cumul matchedcw, gen(cumulmatchedcw)
gen share_matched_swe_smwp = matchedcw if cumulmatchedcw<0.5 & cumulmatchedcw!=.
gen share_matched_swe_bigwp = matchedcw if cumulmatchedcw>0.5 & cumulmatchedcw!=.
gen share_cross_bigwp= crossskill if cumulmatchedcw>0.5 & cumulmatchedcw!=.
gen share_cross_smallwp= crossskill if cumulmatchedcw<0.5 & cumulmatchedcw!=.


/////////////////////////////////////////
//FOR DESCRIPTIVE PURPOSES
///////////////////////////////////////

///how long have someone stayed in Sweden
gen yearincountry = `y'-im_year if IFAUkod!=0 & im_year!=0
replace yearincountry=. if yearincountry<0

///four categories of new, medium, established, old
gen new_im = 1 if yearincountry<6 & other==1
gen mediumnew_im =1 if yearincountry>5 & yearincountry<11 & other==1
gen notnew_im =1 if yearincountry>10 & yearincountry<16 & other==1
gen old_im = 1 if yearincountry>15 & other==1

///per workplace
bysort ArbstId: egen newim_coworker = total(new_im)
bysort ArbstId: egen mediumnewim_coworker = total(mediumnew_im)
bysort ArbstId: egen notnewim_coworker = total(notnew_im)
bysort ArbstId: egen oldim_coworker = total(old_im)

///now condition the created workplace variables on being swedish
foreach x in newim mediumnewim notnewim oldim  {
gen share_`x'_cworker = `x'_coworker/coworkers
gen share_`x'_cworker_swe = share_`x'_cworker if IFAUkod==0
gen `x'_coworker_swe = `x'_coworker if IFAUkod==0
}


////////////////
///ROBUSTNESS
//////////////////

//FIXED EFFECTS FOR INDUSTRY SECTOR

/// National sector-number - want a number for im_workers in each sector nationally

/// Use 2002 for 2006, since 2007 version not available for 2006, 
/// use 2-digits (about 60 sectors)
tostring AstSNI, gen(AstSNI_str)
gen sector_2dgt = substr(AstSNI_str, 1, 2)

///#employees in sector, #imm employees in sector
bysort sector_2dgt: gen sector_empl = _N
bysort sector_2dgt: egen sector_empl_im = total(other)
gen sector_share_im =  sector_empl_im/sector_empl

///Create fixed effects for sector
gen deviation_sector = sh_im_cw_noi_swe-sector_share_im 




///////////////////////////////
//USE THE OFFSHORING DATA TO GET
//INFO ON ROUTINE TASK JOBS
////////////////////////////


///use first 2 digit in SSYK
capture gen SSYK_str2 = substr(SSYK3, 1,2)
capture gen SSYK_str2 = substr(SSYK4_96, 1,2)

rename SSYK_str2 SSYK2

merge m:1 SSYK2 using "E:\ProjData\IntegrationSD\raw\RTI_and_offshoring.dta" 
drop _merge

rename SSYK2 SSYK_str2

encode SSYK_str2, gen(SSYK2)

///per workplace - create 2 digit SSYK variable
tab SSYK2, gen(SSYK2)

foreach b of numlist 1(1)28 {
gen SSYK2`b'other = SSYK2`b' if other==1
}

foreach b of numlist 1(1)28 {
bysort ArbstId: egen SSYK2`b'_coworker = total(SSYK2`b')
bysort ArbstId: egen SSYK2`b'_othercw = total(SSYK2`b'other)
}

///drop oneself, so we divide with CO-workers
foreach b of numlist 1(1)28 {
replace SSYK2`b'_coworker = SSYK2`b'_coworker-1 if SSYK2`b' ==1 
}

///now take share and condition the created workplace variables on being swedish
foreach b of numlist 1(1)28 {
gen share_ssyk2`b'_cworker = SSYK2`b'_othercw/SSYK2`b'_coworker
gen share_ssyk2`b'_cworker_swe = share_ssyk2`b'_cworker if IFAUkod==0
}

///one variable
gen share_ssyk2=.

foreach b of numlist 1(1)28 {
replace share_ssyk2 = share_ssyk2`b'_cworker_swe if SSYK2`b' ==1 
}

///not in the same occupation code
gen non_ssyk2 =.
foreach b of numlist 1(1)28 {
replace non_ssyk2 = im_coworker-SSYK2`b'_othercw if SSYK2`b' ==1 & IFAUkod==0
}

///standardize with all coworkers not in the same occupation
foreach b of numlist 1(1)28 {
replace SSYK2`b'_coworker = SSYK2`b'_coworker+1 if SSYK2`b' ==1 
}

gen non_ssyk2_all =.
foreach b of numlist 1(1)28 {
replace non_ssyk2_all = coworkers-SSYK2`b'_coworker if SSYK2`b' ==1 & IFAUkod==0
}
gen share_non_ssyk2 = non_ssyk2/non_ssyk2_all


///missing info on some + militaries (no RTI) and politicians -  
replace share_ssyk2=. if SSYK21==1 | SSYK22==1 | SSYK23==1
replace share_ssyk2=. if occupation==.
replace share_non_ssyk2=. if SSYK21==1 | SSYK22==1 | SSYK23==1
replace share_non_ssyk2=. if occupation==.

///RTI codes: 
gen share_topRTI = sh_im_cw_noi_swe if RTI_group==1 | RTI_group==2
gen share_midtopRTI = sh_im_cw_noi_swe if RTI_group==3 | RTI_group==4
gen share_midbottomRTI = sh_im_cw_noi_swe if RTI_group==5 | RTI_group==6
gen share_bottomRTI = sh_im_cw_noi_swe if RTI_group==7 | RTI_group==8

gen share_ssyk2_topRTI = share_ssyk2 if RTI_group==1 | RTI_group==2
gen share_ssyk2_midtopRTI = share_ssyk2 if RTI_group==3 | RTI_group==4
gen share_ssyk2_midbottomRTI = share_ssyk2 if RTI_group==5 | RTI_group==6
gen share_ssyk2_bottomRTI = share_ssyk2 if RTI_group==7 | RTI_group==8

gen share_matched_topRTI = matchedcw if RTI_group==1 | RTI_group==2
gen share_cross_topRTI = crossskill if RTI_group==1 | RTI_group==2
gen share_matched_bottomRTI = matchedcw if RTI_group==7 | RTI_group==8
gen share_cross_bottomRTI = crossskill if RTI_group==7 | RTI_group==8




//big and small workplaces INTERACTED WITH SKILL LEVEL
gen share_ssyk2_big= share_ssyk2 if cumulworkers>0.5 & cumulworkers!=.
gen share_ssyk2_small= share_ssyk2 if cumulworkers<0.5 & cumulworkers!=.
gen share_non_ssyk2_big= share_non_ssyk2 if cumulworkers>0.5 & cumulworkers!=.
gen share_non_ssyk2_small= share_non_ssyk2 if cumulworkers<0.5 & cumulworkers!=.




*oTHER

///total workers
gen workers = 1 
gen workers_swe = 1 if IFAUkod==0



///keep
keep LopNr im_coworker coworkers share_im_cworker workers workers_swe ///
share_im_cworker_swe im_coworker_swe coworkers_swe ArbstId ///
Sektorkod deviation_sector sector_share_im share_matched_swe_smwp share_matched_swe_bigwp ///
latin_coworker mena_coworker asia_coworker africa_coworker rest_coworker nordic_coworker western_coworker ///
share_latin_cworker share_latin_cworker_swe latin_coworker_swe share_mena_cworker /// 
share_mena_cworker_swe mena_coworker_swe share_asia_cworker share_asia_cworker_swe ///
asia_coworker_swe share_africa_cworker share_africa_cworker_swe africa_coworker_swe ///
share_rest_cworker share_rest_cworker_swe rest_coworker_swe share_nordic_cworker ///
share_nordic_cworker_swe nordic_coworker_swe share_western_cworker ///
share_western_cworker_swe western_coworker_swe new_im mediumnew_im notnew_im old_im ///
yearincountry newim_coworker mediumnewim_coworker notnewim_coworker oldim_coworker /// 
share_newim_cworker share_newim_cworker_swe newim_coworker_swe ///
share_mediumnewim_cworker share_mediumnewim_cworker_swe mediumnewim_coworker_swe ///
share_notnewim_cworker share_notnewim_cworker_swe notnewim_coworker_swe ///
share_oldim_cworker share_oldim_cworker_swe oldim_coworker_swe ///
high_skilled low_skilled highsk_coworker lowsk_coworker highsk_other lowsk_other ///
highskother_coworker lowskother_coworker share_highsk_cworker share_highsk_cworker_swe ///
highsk_coworker_swe share_lowsk_cworker share_lowsk_cworker_swe lowsk_coworker_swe ///
share_highskother_cworker share_highskother_cworker_swe highskother_coworker_swe ///
share_lowskother_cworker share_lowskother_cworker_swe lowskother_coworker_swe ///
matchedcoworkers share_matchedhigh_cworker share_matchedlow_cworker ///
sharecrossskill_highnative sharecrossskill_lownative crossskill matchedcoworkers_all share_matchedhigh_allcworker ///
share_matchedlow_allcworker share_im_cworker_swe_smwp share_im_cworker_swe_bigwp ///
share_young_cw_wp share_male_cw_wp ///
share_ssyk2 wage_cw_wp share_ssyk2_topRTI share_ssyk2_bottomRTI ///
 share_ssyk2_midbottomRTI share_ssyk2_midtopRTI share_non_ssyk2 share_ssyk2_big ///
 share_ssyk2_small share_non_ssyk2_big share_non_ssyk2_small stdRTI matchedcw sh_im_cw_noi_swe ///
 share_topRTI share_midtopRTI share_midbottomRTI share_bottomRTI share_ssyk1 share_non_ssyk1 ///
 share_matched_big share_matched_small share_cross_big share_cross_small share_matched_topRTI ///
 share_cross_topRTI share_matched_bottomRTI share_cross_bottomRTI share_cross_bigwp share_cross_smallwp
 
 
///Left with: 
///check amount of workplaces
*bysort ArbstId: gen diff_jobs = _n
*count if diff_jobs==1
*2006: 3,604,076 workers connected to a workplace id 192,088 workplaces
*2010: 3,684,633 workers connected to a workplace id 203,576 workplaces
*2014: 3,852,892 workers connected to a workplace id 211,953 workplaces

save "E:\ProjData\IntegrationSD\temp\workplace_`y'.dta", replace

}




///FOR ROBUSTNESS, CREATE A BASELINE MEASURE WITH NO RESTRICTIONS ON SAMPLE
*WORKPLACE DATA FOR 2006, 2010 AND 2014
forvalues y = 2006(4)2014 {

use "E:\ProjData\IntegrationSD\temp\LISA`y'.dta"

*STILL HAS TO DROP THIS
drop if ArbstId==.

///throw out duplicates
duplicates tag LopNr, gen(tag)
tab tag
drop if tag==1
*2006: none
*2010: 2 ind/4 obs deleted*
*2014: 2 ind/4 obs deleted*
drop tag


///ADD Birth country data, need to define foreign born/non-European
merge 1:1 LopNr using "RTB\FodelselandIFAUGrupp"
	keep if _merge==3 
	drop _merge
	

////////////////////////////////////////////
////NOW THE FOLLOWING LINE OF CODES CREATES 
///THE MAIN TREATMENT; SHARE OF IM 
//// COWORKERS AMONG SWEDED
////////////////////////////////////////////	


///Start by defining being a native or comming from certain region, used to 
*consider type of coworker
gen swedish = 1 if IFAUkod==0
gen other =  1 if IFAUkod >39 & IFAUkod < 54

///Now workplace codes, by workplace (ArbstId) , to gen # im coworkers vs # coworkers per workplace
*all immigrant workers at a workplace
bysort ArbstId: egen im_coworker = total(other)
*all workers at a workplace
bysort ArbstId: gen coworkers = _N

///now condition the created workplace variables on being swedish
*this creates the main treatment variable, share of (im)coworkers if you are Swedish
foreach x in im  {
gen `x'_coworker_swe = `x'_coworker if IFAUkod==0
}

*Total coworkers if you are swedish
gen coworkers_swe = coworkers if IFAUkod==0

*to create "CO"-workers, rather than workers, take coworkers - oneself; 
* "coworkers" otherwise captures all workers
gen cw_not_i = coworkers-1
gen sh_im_cw_noi = im_coworker/cw_not_i
gen sh_im_cw_noi_swe_rob = sh_im_cw_noi if IFAUkod==0


///keep
keep LopNr sh_im_cw_noi_swe_rob 
 
///Left with: 
///check amount of workplaces
*bysort ArbstId: gen diff_jobs = _n
*count if diff_jobs==1
*2006: 3,604,076 workers connected to a workplace id 192,088 workplaces
*2010: 3,684,633 workers connected to a workplace id 203,576 workplaces
*2014: 3,852,892 workers connected to a workplace id 211,953 workplaces

save "E:\ProjData\IntegrationSD\temp\workplace_`y'_rob.dta", replace

erase "E:\ProjData\IntegrationSD\temp\LISA`y'.dta"
}

